package com.zcn.io.web.service;

import com.github.benmanes.caffeine.cache.Cache;
import com.zcn.io.web.auth.LoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/10/2 20:57
 * @Copyright (c) 2024, 江苏金恒信息科技股份有限公司
 */
@Service

public class CacheService {
    @Autowired
    private Cache<String, Object> caffeineCache;

    public void putInCache(String key, Object value) {
        caffeineCache.put(key, value);
    }

    public Object getFromCache(Object key) {
        return caffeineCache.getIfPresent(key); // 如果key不存在，返回null
    }

    public LoginUser getLoginUserFromCache(Object key) {
        Object o = caffeineCache.getIfPresent(key);
        if (o instanceof LoginUser) {
            return (LoginUser) o;
        }
        return null;
    }
}
